//
//  PopoverBaseManager.swift
//  XiaoweHearingAid
//
//  Created by edy on 2024/12/12.
//

import Foundation

extension PopoverBaseManager {
    
    class func showMenuListView(fromView: UIView, inView: UIView, popoverType: PopoverType, menuItemType: XWMenuListItemType, menuItemTitleList: [String], menuItemIconList: [UIImage], itemWidth: CGFloat = 120, clickAction: @escaping MenuListItemClickAction) {
        if menuItemTitleList.isEmpty {
            return
        }
        
        switch menuItemType {
        case .onlyTitle:
            break
            
        case .leftTitleRightIcon:
            if menuItemIconList.count != menuItemTitleList.count {
                return
            }
                
        case .leftIconRightTitle:
            if menuItemIconList.count != menuItemTitleList.count {
                return
            }
        }
        var menuItemList: [XWMenuListItemModel] = []
        
        for i in 0..<menuItemTitleList.count {
            let iItem = XWMenuListItemModel()
            iItem.title = menuItemTitleList[i]
            iItem.itemType = menuItemType
            
            if menuItemType != .onlyTitle {
                iItem.icon = menuItemIconList[i]
            }
            
            menuItemList.append(iItem)
        }
        
        showMenuListView(fromView: fromView, inView: inView, popoverType: popoverType, menuItemList: menuItemList, itemWidth: itemWidth, showBlackOverlay: false, clickAction: clickAction)
    }
    
    class func showMenuListView(fromView: UIView, inView: UIView, popoverType: PopoverType, menuItemList: [XWMenuListItemModel], itemWidth: CGFloat, showBlackOverlay: Bool, clickAction: @escaping MenuListItemClickAction) {
        showMenuListView(fromView: fromView, inView: inView, popoverType: popoverType, menuItemList: menuItemList, sIndex: nil, itemWidth: itemWidth, showBlackOverlay: showBlackOverlay, clickAction: clickAction)
    }
    
    class func showMenuListView(fromView: UIView, inView: UIView, popoverType: PopoverType, menuItemList: [XWMenuListItemModel], sIndex: Int?, itemWidth: CGFloat, showBlackOverlay: Bool, clickAction: @escaping MenuListItemClickAction) {
//        let width: CGFloat = itemWidth
//        var height: CGFloat = 160
//        let cellHeight: CGFloat = 40
//        if menuItemList.count <= 5 {
//            height = cellHeight * menuItemList.count.cgFloat
//        }
//        let contentView = XWMenuListBaseView(frame: CGRect(x: 0, y: 0, width: width, height: height))
//        contentView.sIndex = sIndex
//        contentView.itemList = menuItemList
//        contentView.clickAction = { [weak contentView] aType, selectIndex, selectValue in
//            clickAction(.confirm, selectIndex, selectValue)
//            dismissPopoover(contentView: contentView)
//        }
//        
//        let options = [
//          .type(popoverType),
//          .subType(.right),
//          .animationIn(0.3),
//          .arrowSize(CGSize.zero),
//          .showBlackOverlay(showBlackOverlay)
//          ] as [PopoverOption]
//        
//        showPopover(popoverOption: options, contentView: contentView, fromView: fromView, inView: inView) {
//            clickAction(.cancel, 0, XWMenuListItemModel())
//        }
        showMenuListView(fromView: fromView, inView: inView, popoverType: popoverType, subType: .right, menuItemList: menuItemList, sIndex: sIndex, itemWidth: itemWidth, showBlackOverlay: showBlackOverlay, clickAction: clickAction)
    }
    
    class func showMenuListView(fromView: UIView, inView: UIView, popoverType: PopoverType, subType: PopoverSubType, menuItemList: [XWMenuListItemModel], sIndex: Int?, itemWidth: CGFloat, showBlackOverlay: Bool, clickAction: @escaping MenuListItemClickAction) {
        let width: CGFloat = itemWidth
        let cellHeight: CGFloat = 40
        let maxItem = 8
        var height: CGFloat = cellHeight * maxItem.double
        if menuItemList.count <= maxItem {
            height = cellHeight * menuItemList.count.cgFloat
        }
        let contentView = XWMenuListBaseView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        contentView.sIndex = sIndex
        contentView.itemList = menuItemList
        contentView.clickAction = { [weak contentView] aType, selectIndex, selectValue in
            clickAction(.confirm, selectIndex, selectValue)
            dismissPopoover(contentView: contentView)
        }
        
        let options = [
          .type(popoverType),
          .subType(subType),
          .animationIn(0.3),
          .arrowSize(CGSize.zero),
          .showBlackOverlay(showBlackOverlay)
          ] as [PopoverOption]
        
        showPopover(popoverOption: options, contentView: contentView, fromView: fromView, inView: inView) {
            clickAction(.cancel, 0, XWMenuListItemModel())
        }
    }
    
}

class PopoverBaseManager {
    
    class func getDialogPopoverOptions() -> [PopoverOption] {
        let options = [
            .dismissOnBlackOverlayTap(false)
          ] as [PopoverOption]
        return options
    }
    
    class func showPopoverAsDialog(popoverOption: [PopoverOption], contentView: UIView, inView: UIView, dismissHandler: @escaping (() -> ())) {
        showPopoverAsDialog(popoverOption: popoverOption, contentView: contentView, inView: inView, yOffset: 0, dismissHandler: dismissHandler)
    }
    
    class func showPopoverAsDialog(popoverOption: [PopoverOption], contentView: UIView, inView: UIView, yOffset: CGFloat, dismissHandler: @escaping (() -> ())) {
        let options = popoverOption
        let popover = Popover(options: options, showHandler: nil, dismissHandler: dismissHandler)
        popover.showAsDialog(contentView, inView: inView, yOffset: yOffset)
    }
    
    
    class func getDefautPopoverOptions() -> [PopoverOption] {
        let options = [
          .type(.down),
          .subType(.right),
          .animationIn(0.3),
          .arrowSize(CGSize.zero),
          .showBlackOverlay(false)
          ] as [PopoverOption]
        
        return options
    }
    
    class func showPopover(popoverOption: [PopoverOption], contentView: UIView, fromView: UIView, inView: UIView, dismissHandler: @escaping (() -> ())) {
        let options = popoverOption
        let popover = Popover(options: options, showHandler: nil, dismissHandler: dismissHandler)
//        popover.didDismissHandler = {
//            dismissHandler()
//        }
        popover.show(contentView, fromView: fromView, inView: inView)
    }
    
    class func dismissPopoover(contentView: UIView?) {
        guard let popover = contentView?.superview as? Popover else {
            return
        }
        popover.didDismissHandler = nil
        popover.dismiss()
    }
    
}
